﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IBLL.View;
using Model.Controller;
using Model;

namespace CenterDateAchieve.Forms
{
    public partial class EditFormBase : FormBase
    {
        public EditFormBase()
        {
            InitializeComponent();
        }
        private const string SingleQuote = "'";
        private const string DoubleQuote = "''";
        public object PrimaryKey { get; set; }

      
        /// <summary>
        /// 重载用于初始化
        /// </summary>
        public virtual void OnInitialization()
        {

        }

        public virtual void OnFinalization()
        {

        }
     
        private void EditFormBase_Load(object sender, EventArgs e)
        {

        }

        #region IEditView 成员



        public virtual void OnAdjustUI(EntityState editStat, IDataTrans data)
        {
             
        }

        /// <summary>
        ///存放新数据到界
        /// </summary>
        public virtual void OnSetDataToView(EntityState editStat, IDataTrans data)
        {
            SetDataToBinding(GetEntity(data),this);
        }

        private void SetDataToBinding(object p,Control c)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is GroupBox || ctrl is Panel || ctrl is TabControl || ctrl is TabPage)
                    SetDataToBinding(p, ctrl);
                else if (ctrl is TextBox)
                {
                    ctrl.Text = GetColValue(ctrl.Name, p).ToString();
                }
                else if (ctrl is ComboBox)
                {
                    ((ComboBox)ctrl).SelectedValue = GetColValue(ctrl.Name, p).ToString();
                }
                else if (ctrl is CheckBox)
                {
                    bool b = false;
                    if (!string.IsNullOrEmpty(GetColValue(ctrl.Name, p).ToString()))
                        b = Convert.ToBoolean(GetColValue(ctrl.Name, p).ToString());
                    ((CheckBox)ctrl).Checked = b;
                }



            }
        }

        private object GetColValue(string p, object obj)
        {
            Entity data = obj as Entity;

            foreach (ColumnDefine col in data.Cols)
            {
                if (col.ControlName == p)
                {
                    return col.Value == null ? string.Empty : col.Value;
                }
            }
            foreach (ColumnDefine col in data.PrimaryKey.Keys)
            {
                if (col.ControlName == p)
                {
                    return col.Value == null ? string.Empty : col.Value;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取数据到界面
        /// </summary>
        public virtual void OnGetFromView(EntityState editStat, IDataTrans data)
        {
            GetDataFromBinding(GetEntity(data),this);


        }

        private void GetDataFromBinding(object p,Control c)
        {
            foreach (Control ctrl in c.Controls)
            {

                if (ctrl is GroupBox || ctrl is Panel || ctrl is TabControl || ctrl is TabPage)
                    GetDataFromBinding(p, ctrl);
                else if (ctrl is TextBox)
                {
                    SetColValue(ctrl.Text, ctrl.Name, p);
                }
                else if (ctrl is ComboBox)
                {
                    SetColValue(((ComboBox)ctrl).SelectedValue, ctrl.Name, p);
                }
                else if (ctrl is CheckBox)
                {
                    SetColValue(((CheckBox)ctrl).Checked, ctrl.Name, p);

                }



            }
        }

        private void SetColValue(object p, string ctrlName, object obj)
        {
            Entity data = obj as Entity;
            if (p != null && p.ToString().Contains(SingleQuote))
            {
                p = p.ToString().Replace(SingleQuote, DoubleQuote);
            }
            int length = data.PrimaryKey.Keys.Count;
               
            for (int i = 0; i < length; i++)
            {
                if (data.PrimaryKey.Keys[i].ControlName == ctrlName)
                {
                    data.PrimaryKey.Keys[i].Value = p;
                    return;
                }
            }
            int Col_Length = data.Cols.Count;

            for (int i = 0; i < Col_Length; i++)
            {
                if (data.Cols[i].ControlName == ctrlName)
                {
                    data.Cols[i].Value = p;
                    return;
                }
            }

        }
        /// <summary>
        /// 对于entity类型的窗口必须实现
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual object GetEntity(IDataTrans data)
        {
            return ((DataTrans)data).Entity;
        }

        /// <summary>
        /// 错误处理
        /// </summary>
        public void ShowError(string error)
        {
            //CheckedExecute(() =>
            //{

            //    throw new RecordControllerException(databindMgr_.Errors.GetError());
            //}
            //   );
        }

        public bool IsConfim(string message)
        {
          DialogResult result=MessageBox.Show(message, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
          if (result == DialogResult.Yes)
              return true;
          else
              return false;
        }


        public void OnClose()
        {
            this.Close();
        }

       
        public void OnShow()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ShowDialog();
        }
        protected void InvokeCreateAction(object sender, EventArgs e)
        {
            if (CreateAction != null)
                CreateAction(sender, e);
        }
        protected void InvokeSaveAction(object sender, EventArgs e)
        {
            if (SaveAction != null)
                SaveAction(sender, e);
        }
        protected void InvokeDeleteAction(object sender, EventArgs e)
        {
            if (DeleteAction != null)
                DeleteAction(sender, e);
        }
        public virtual void RefreshUI(object sender, string data)
        {
           
        }
        public virtual void BeforeSaveAction(IDataTrans data)
        { }
        /// <summary>
        /// 创建动作
        /// </summary>
       public event EventHandler CreateAction;
        /// <summary>
        /// 保存动作
        /// </summary>
       public event EventHandler SaveAction;

        /// <summary>
        /// 删除动作
        /// </summary>
       public event EventHandler DeleteAction;
        #endregion
    }
}
